package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    //定义定时清理图片
    public void clearImg() {
        //获取无效的图片信息
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        if(set != null) {
            for (String imgFileName : set) {
                QiniuUtils.deleteFileFromQiniu(imgFileName);
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,imgFileName);
                System.out.println("正在执行删除图片操作："+new Date());
                jedisPool.close();
            }
        }
    }
}
